package com.colbert.jknife.model;

import java.util.*;

public class CodeSetManager implements ICodeSetManager {

    private Map<String, Map<Integer, FieldCodeSet>> codeSetMap;

    private static CodeSetManager sInstance = new CodeSetManager();

    private CodeSetManager() {
        codeSetMap = new LinkedHashMap<>();
    }

    public static CodeSetManager getInstance() {
        return sInstance;
    }

    private Map<Integer, FieldCodeSet> getOrCreateFieldCodeSetMap(String className) {
        Map<Integer, FieldCodeSet> fieldCodeSets = codeSetMap.get(className);
        if (fieldCodeSets == null) {
            fieldCodeSets = new LinkedHashMap<>();
            codeSetMap.put(className, fieldCodeSets);
        }
        return fieldCodeSets;
    }

    public FieldCodeSet getOrCreateFieldCodeSet(String className, int id) {
        Map<Integer, FieldCodeSet> fieldCodeSetMap = getOrCreateFieldCodeSetMap(className);
        FieldCodeSet fieldCodeSet = fieldCodeSetMap.get(id);
        if(fieldCodeSet == null) {
            fieldCodeSet = new FieldCodeSet();
            fieldCodeSetMap.put(id, fieldCodeSet);
        }
        return fieldCodeSet;
    }

    @Override
    public Map<String, Map<Integer, FieldCodeSet>> getCodeSetMap() {
        return new LinkedHashMap<>(codeSetMap);
    }

    @Override
    public void clear() {
        codeSetMap.clear();
    }
}
